using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerRunningState : PlayerMovingState
{
    private PlayerSprintData sprintData;
    private float startTime;
    public PlayerRunningState(PlayerMovementMachine playerMovementMachine) : base(playerMovementMachine)
    {
        sprintData = StateMachine.Player.Data.PlayerGroundedData.SprintData;
    }
    
#region IState Methods
    public override void Enter()
    {
        base.Enter();
        StartAnimation(StateMachine.Player.AnimationData.RunParameterHash);
        StateMachine.ReusableData.MovementSpeedModify =
            StateMachine.Player.Data.PlayerGroundedData.RunData.SpeedModify;
        StateMachine.ReusableData.CurrentJumpForce = airborneData.JumpData.MediumForce;

    }
    
    public override void Update()
    {
        base.Update();

        if (!StateMachine.ReusableData.ShouldWalk)
            return;

        if (Time.time < startTime + sprintData.RunToWalkTime)
            return;

        StopRunning();
    }

    public override void Exit()
    {
        base.Exit();
        StopAnimation(StateMachine.Player.AnimationData.RunParameterHash);
    }
#endregion

#region Main Methods
    private void StopRunning()
    {
        if (StateMachine.ReusableData.InputMovement == Vector2.zero)
        {
            StateMachine.ChangeState(StateMachine.IdlingState);
            return;
        }
        
        StateMachine.ChangeState(StateMachine.WalkingState);
    }
#endregion

#region Input Methods
    protected override void OnMovementCanceled(InputAction.CallbackContext context)
    {
        StateMachine.ChangeState(StateMachine.MediumStoppingState);
    }
    
    protected override void OnWalkToggleStated(InputAction.CallbackContext context)
    {
        base.OnWalkToggleStated(context);
        StateMachine.ChangeState(StateMachine.WalkingState);
    }
#endregion
}
